// import { goodsApi } from "@/utils/api";

import goods from "@/mock/goods";

interface User {
  date: string;
  name: string;
  address: string;
  price: string;
  Tick: boolean;
  stock: string;
  sales: string;
  data: string;
  state: string;
}
export const goodsModule = {
  namespaced: true,
  state: {
    goodsList: [],
    count: +"",
  },
  mutations: {
    // 数据列表
    getstategoods(state: any, payload: any) {
      state.goodsList = payload.result.map((item: any) => {
        return {
          ...item,
          modify: false,
        };
      });
      state.count = payload.count;
    },

    // 批量下架按钮
    offAll(state: any) {
      state.goodsList.map((item: User) => {
        if (item.Tick) {
          item.state = "已下架";
          return item;
        }
      });
    },
    // 批量上架按钮
    upperAll(state: any) {
      state.goodsList.map((item: User) => {
        if (item.Tick) {
          console.log(item.Tick);
          item.state = "销售中";
        }
      });
    },
    // 批量删除按钮
    removeAll(state: any) {
      state.goodsList = state.goodsList.filter((item: any) => {
        return item.Tick === false;
      });
    },
    // 删除按钮
    remove(state: any, date: any) {
      // const i = state.goodsList.findIndex((x: any) => x.date === payload);
      // 根据索引，删除对应的元素
      console.log(date);
      state.goodsList = state.goodsList.filter((item: any) => {
        return item.date !== date;
      });
    },

    //全部勾选触发
    selectAll(state: any, payload: any) {
      state.goodsList.forEach((item: any) => {
        if (Object.keys(payload).length !== 0) {
          item.Tick = true;
        } else {
          item.Tick = false;
        }
      });
    },
    // 编辑完成修改数据按钮
    success(state: any, payload: any) {
      state.goodsList.forEach((item: any) => {
        if (item.date === payload.row.date) {
          item.name = payload.new1.newname;
          item.address = payload.new1.newaddress;
          item.date = payload.new1.newdate;
          item.price = payload.new1.newprice;
          item.stock = payload.new1.newstock;
          item.sales = payload.new1.newsales;
          item.data = payload.new1.newdata;
        }
      });
    },

    // 查询
    query(state: any, obj: any) {
      console.log(obj.queryObj);

      state.goodsList = state.goodsList.filter((item: any) => {
        const newobj = Object.keys(obj);
        return newobj.every((value: any) => {
          return obj[value] === item[value] || !obj[value];
        });
      });
    },
  },
  actions: {
    async getgoodsAsync({ commit }: any, pageSize: any) {
      const res: any = await goods.getData(pageSize);

      commit("getstategoods", res);
    },
  },
  getters: {},
};
